package com.hz.ruoyiequipment.equipment.controller;

import com.hz.ruoyiequipment.equipment.domain.AgrEnvRegulation;
import com.hz.ruoyiequipment.equipment.domain.AgrEquipmentFertigation;
import com.hz.ruoyiequipment.equipment.service.AgrEnvRegulationService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;


/**
 * @author 高铭月
 * @date 2025/3/7 11:35
 * @description:
 */
@RestController
@RequestMapping("/regulation")
public class AgrEnvRegulationController extends BaseController {
    @Autowired
    private AgrEnvRegulationService agrEnvRegulationService;

    @Autowired
    private ScheduledExecutorService scheduledExecutorService;
    @GetMapping("/list")
    @RequiresPermissions("equipment:regulation:list")
    public TableDataInfo getAgrEnvRegulation(AgrEnvRegulation agrEnvRegulation) {
        startPage();
        List<AgrEnvRegulation> list = agrEnvRegulationService.getAgrEnvRegulation(agrEnvRegulation);
        return getDataTable(list);
    }

    @GetMapping("/detail")
    @RequiresPermissions("equipment:regulation:detail")
    public AjaxResult getAgrEnvRegulationByEquipmentId(Long equipmentId) {
        return agrEnvRegulationService.getAgrEnvRegulationByEquipmentId(equipmentId);
    }

    @PostMapping("/updateStatus")
    @RequiresPermissions("equipment:regulation:toggle")
    public AjaxResult updateStatus(@RequestBody Map<String, Object> params) {
        Long equipmentId = Long.valueOf(params.get("equipmentId").toString());
        Integer status = Integer.valueOf(params.get("status").toString());
        return agrEnvRegulationService.updateStatus(equipmentId, status);
    }

    @PostMapping("/insert")
    @RequiresPermissions("equipment:regulation:add")
    public AjaxResult insertRegulationEquipment(@RequestBody AgrEnvRegulation regulation) {
        return agrEnvRegulationService.insertRegulationEquipment(regulation);
    }

    @PostMapping("/update")
    @RequiresPermissions("equipment:regulation:edit")
    public AjaxResult updateRegulationEquipment(@RequestBody AgrEnvRegulation regulation) {
        return agrEnvRegulationService.updateRegulationEquipment(regulation);
    }

    @RequiresPermissions("equipment:regulation:delete")
    @PostMapping("/delete")
    public AjaxResult delRegulationEquipmentByEquipmentId(Long equipmentId) {
        return agrEnvRegulationService.delRegulationEquipment(equipmentId);
    }
    /**
     * @param exeTime
     * @param equipmentId
     * @return: com.ruoyi.common.core.web.domain.AjaxResult
     * @Author: zjk
     * @Date: 2025/3/19 8:58
     * @Description: 预警 触发气象设备自动关闭
     */

    @PostMapping("/scheduleCountdown")
    public AjaxResult scheduleCountdownEq(@RequestParam("exeTime") Double exeTime, @RequestParam("equipmentId") Long equipmentId) {
        if (exeTime  <= 0){
            return AjaxResult.error("环境调控设备执行时长异常,参数错误");
        }
        long delay = (long) (exeTime * 60 * 1000);
        scheduledExecutorService.schedule(() -> {
            try {
                agrEnvRegulationService.updateStatus(equipmentId, 0);
            } catch (Exception e) {
                e.printStackTrace();
            }
            // do something
        }, delay, TimeUnit.MILLISECONDS);
        return AjaxResult.success();
    }
    /**
     * @param equipmentId
     * @param equipmentId
     * @return: com.ruoyi.common.core.web.domain.AjaxResult
     * @Author: zjk
     * @Date: 2025/3/19 8:58
     * @Description: 预警 根据气象设备id查状态
     */
    @PostMapping("/inner")
    public Integer getAgrEnvRegulationByEquipmentIdInner(@RequestParam("equipmentId") Long equipmentId) {
        return agrEnvRegulationService.getAgrEnvRegulationStatusByEquipmentIdInner(equipmentId);
    }

}
